package com.example.controller;

import com.example.pojo.Event;
import com.example.pojo.PageBean;
import com.example.pojo.Result;
import com.example.pojo.User;
import com.example.service.EventService;
import com.example.vo.EventVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

@Api(tags = "赛事管理模块")
@Slf4j
@RestController
@RequestMapping("/api/events")
public class EventController {

    @Autowired
    private EventService eventService;


    @ApiOperation("根据eventID查询赛事,路径参数")
    @GetMapping("/{eventID}")
    public Result getById(@PathVariable Integer eventID) {
        log.info("根据eventID查询赛事信息,eventID:{}",eventID);
        EventVO eventVO = eventService.getById(eventID);
        return Result.success(eventVO);
    }

    @ApiOperation("根据eventID批量删除赛事,路径参数，集合")
    @DeleteMapping("/{eventIDs}")
    public Result delete(@PathVariable List<Integer> eventIDs) throws Exception {
        log.info("批量删除操作,eventIDs{}",eventIDs);
        Result result = eventService.deleteByIDs(eventIDs);
        return result;
    }
    @ApiOperation("新增赛事数据")
    @PostMapping
    public Result save(@RequestBody Event event){
        log.info("新增赛事数据,event:{}",event);
        eventService.save(event);
        return Result.success();
    }

    @ApiOperation("更新赛事数据")
    @PutMapping
    public Result update(@ApiParam("根据eventID找到并更新用户") @RequestBody Event event){
        eventService.update(event);
        return Result.success();
    }
    @ApiOperation("赛事分页查询，其中赛事名称支持模糊查询,返回结果是比赛开始时间的升序")
    @GetMapping
    public Result page(@ApiParam("要查询第几页，默认是第一页") @RequestParam(defaultValue = "1") Integer page,
                       @ApiParam("每页要查询几条数据，默认是10")@RequestParam(defaultValue = "10") Integer pageSize,
                       @ApiParam("赛事的名字，支持模糊查询")String eventName,
                       @ApiParam("赛事的地点，1-1号场馆，2-2号场馆") Short place,
                       @ApiParam("是否正在销售，1-正在销售 2-停止销售") Short onSale,
                       @ApiParam("用户ID") Integer userID,
                       @ApiParam("比赛开始时间的起始范围 pattern = yyyy-MM-dd") @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                       @ApiParam("比赛开始时间的截至范围 pattern = yyyy-MM-dd") @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end){
        log.info("赛事分页查询，参数：{},{},{},{},{},{},{}",page,pageSize,eventName,place,onSale,begin,end);
        //调用service分页查询
        PageBean pageBean = eventService.page(page,pageSize,eventName,place,onSale,userID,begin,end);
        return Result.success(pageBean);

    }
    @ApiOperation("赛事停售")
    @PutMapping("/haltsale")
    public Result haltSale(@RequestParam Integer eventID){

        eventService.haltSale(eventID);
        return Result.success();
    }
    @ApiOperation("赛事起售")
    @PutMapping("/onsale")
    public Result onSale(@RequestParam Integer eventID){

        eventService.onSale(eventID);
        return Result.success();
    }
}
